package xyz.syyrjx.provider.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.syyrjx.client.SyyrjxRpcClient;
import xyz.syyrjx.initializer.SyyrjxRpcChannelInitializer;
import xyz.syyrjx.provider.conf.SyyrjxRpcProviderConfiguration;
import xyz.syyrjx.provider.handler.SyyrjxRpcProviderClientHandler;
import xyz.syyrjx.util.SyyrjxRpcRandomUtil;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/19 - 20:38
 */
public class SyyrjxRpcProviderHeartClientStartUtil {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcProviderHeartClientStartUtil.class);

    public static void start(SyyrjxRpcClient client) throws InterruptedException {
        String[] registryAddress = SyyrjxRpcProviderConfiguration.getConfiguration().getRegistryAddress();
        String[] address = registryAddress[SyyrjxRpcRandomUtil.randomInt(registryAddress.length)].split(":");
        String registryHost = address[0];
        int registryPort = Integer.parseInt(address[1]);
        client.setRemoteHost(registryHost);
        client.setRemotePort(registryPort);
        LOGGER.debug("要连接的注册中心为" + client);

        SyyrjxRpcChannelInitializer channelInitializer = new SyyrjxRpcChannelInitializer();
        channelInitializer.setMaxReadSize(SyyrjxRpcProviderConfiguration.getConfiguration().getMaxReadSize());
        channelInitializer.setHandler(new SyyrjxRpcProviderClientHandler());
        client.run(channelInitializer);
    }
}
